<template>
  <!-- 状态：
  1. 提示加载中....
  2. 提示所有数据加载完成 -->
  <!-- <div ref="target" style="width:200px;border:1px solid #ccc;">
    我是无限加载组件，如果你能看见我，就去请求数据吧
    实时监视本组件是否可见，可见就抛出事件 'on-load'
    <div v-if="isLoading">提示加载中.... </div>
    <div v-if="isFinished">所有数据加载完成....</div>
  </div> -->

  <div class="xtx-infinite-loading" ref="target">
    <!-- 正在加载数据时显示 -->
    <div class="loading" v-if="isLoading">
      <span class="img"></span>
      <span class="text">正在加载...</span>
    </div>
    <!-- 数据全部加载完毕时显示 -->
    <div class="none" v-if="isFinished">
      <span class="text">亲，没有更多了</span>
    </div>
  </div>
</template>
<script>
import { ref } from 'vue'
import { useIntersectionObserver } from '@vueuse/core'
export default {
  name: 'XtxInfinteLoading',
  props: {
    isLoading: { type: Boolean, default: true }, // 是否正在加载
    isFinished: { type: Boolean, default: false } // 并没有全部加载完成
  },
  setup (props, { emit }) {
    const target = ref(null)
    useIntersectionObserver(target,
      ([{ isIntersecting }], dom) => {
        if (isIntersecting && props.isLoading === false && props.isFinished === false) {
          emit('load')
        }
        console.log('是否可见', isIntersecting)
      })

    return { target }
  }
}
</script>

<style scoped lang='less'>
.xtx-infinite-loading {
  .loading {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 200px;
    .img {
      width: 50px;
      height: 50px;
      background: url('~@/assets/images/load.gif') no-repeat center / contain;
    }
    .text {
      color: #999;
      font-size: 16px;
    }
  }
  .none {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 200px;
    .text {
      color: #999;
      font-size: 16px;
    }
  }
}
</style>
